package edu.hit.stu.util.jwt;

import lombok.Getter;

/**
 * @author 刘智彬
 */
@Getter
public enum JwtStatus {

    /**
     * {@code 501 令牌过期}.
     * @see <a href="https://tools.ietf.org/html/rfc6585#section-6">Additional HTTP Status Codes</a>
     */
    EXPIRED(501, "令牌已过期!"),

    /**
     * {@code 502 令牌签名不匹配}
     * @see org.springframework.http.HttpStatus
     */
    NOT_MATCH(502, "签名不匹配!"),

    /**
     * {@code 200 令牌签名匹配成功}
     * @see org.springframework.http.HttpStatus
     */
    MATCH(200, "令牌签名匹配成功!");

    private int code;
    private String msg;

    JwtStatus(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static JwtStatus valueOf(int code) {
        JwtStatus status = resolve(code);
        if (status == null) {
            throw new IllegalArgumentException("没有匹配的 [" + code + "]");
        }
        return status;
    }

    private static JwtStatus resolve(int code) {
        for (JwtStatus value : values()) {
            if (value.code == code) {
                return value;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "该令牌的验证状态为:\n" +
                "\t状态码:\t" + code +
                "\n\t描述信息:\t" + msg;
    }
}
